package com.example.vueapi.vueapi.controller;

import com.example.vueapi.vueapi.request.OrderDto;
import com.example.vueapi.vueapi.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * @ Author     ：jujh
 * @ Date       ：Created in 9:15 2018/10/19
 * @ Description：
 */
@Controller
@Slf4j
public class PayController {

    @GetMapping("/pay")
    public String index() {
        return "pay";
    }

    @RequestMapping("/admin/goPayMoneyWft")
    public String gopay() throws IllegalAccessException {
        OrderDto orderDto = new OrderDto();
        orderDto.setAppid("wx8cee77e017c99ce3");
        orderDto.setMch_id("1487912512");
        orderDto.setNonce_str(RandomUtil.generateString(30));
        // orderDto.setSign("3");
        orderDto.setBody("支付测试-超市");
        orderDto.setOut_trade_no("acb123qwer1234");
        orderDto.setTotal_fee("1");
        orderDto.setSpbill_create_ip("127.0.0.1");
        orderDto.setNotify_url("1");
        orderDto.setTrade_type("JSAPI");
        orderDto.setOpenid("oKEVm0r4h66tpu9W51f9QpC0tAVo");
        orderDto.setSign_type("MD5");
        Map<String, String> paramMap = ObjectHelper.objectToMap(orderDto);
        Map<String, String> params = SignUtils.paraFilter(paramMap);
        StringBuilder buf = new StringBuilder((params.size() + 1) * 10);
        SignUtils.buildPayParams(buf, params, false);
        String preStr = buf.toString();
        String sign = MD5.sign(preStr, "&key=" + "key12313123", "utf-8").toUpperCase();
        paramMap.put("sign", sign);
        Map<String, String> resultMap = PayHttpClient.doRequest("https://api.mch.weixin.qq.com/pay/unifiedorde", paramMap);
        System.out.println(resultMap);
        //String xmlParams = XmlUtils.parseXML(paramMap);
        //System.out.println(xmlParams);
        return "payok";
    }

    public static void main(String[] args) throws IllegalAccessException {
        OrderDto orderDto = new OrderDto();
        orderDto.setAppid("wxc8ffc0376b72218c");
        orderDto.setMch_id("1487912512");
        orderDto.setNonce_str(RandomUtil.generateString(30));
        // orderDto.setSign("3");
        orderDto.setBody("支付测试-超市");
        orderDto.setOut_trade_no("acb123qwer1234");
        orderDto.setTotal_fee("1");
        orderDto.setSpbill_create_ip("127.0.0.1");
        orderDto.setNotify_url("1");
        orderDto.setTrade_type("JSAPI");
        orderDto.setOpenid("oKEVm0r4h66tpu9W51f9QpC0tAVo");
        orderDto.setSign_type("MD5");
        Map<String, String> paramMap = ObjectHelper.objectToMap(orderDto);
        Map<String, String> params = SignUtils.paraFilter(paramMap);
        StringBuilder buf = new StringBuilder((params.size() + 1) * 10);
        SignUtils.buildPayParams(buf, params, false);
        String preStr = buf.toString();
        System.out.println(preStr);
        String sign = MD5.sign(preStr, "&key=" + "76ee772ad7cb2123f9d6a6627c2c5822", "utf-8").toUpperCase();
        paramMap.put("sign", sign);
        String xmlParams = XmlUtils.parseXML(paramMap);
        System.out.println(xmlParams);
        System.out.println(paramMap);
        //Map<String, String> resultMap = PayHttpClient.doRequest("https://api.mch.weixin.qq.com/pay/unifiedorde", paramMap);
      //  System.out.println(resultMap);
        System.out.println("====================================================");
    }
}
